package org.jeecg.common.util.dingtalk;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dingtalk.api.request.OapiCspaceAddToSingleChatRequest;
import com.dingtalk.api.request.OapiFileUploadSingleRequest;
import com.dingtalk.api.request.OapiMediaUploadRequest;
import com.dingtalk.api.response.OapiCspaceAddToSingleChatResponse;
import com.dingtalk.api.response.OapiFileUploadSingleResponse;
import com.dingtalk.api.response.OapiMediaUploadResponse;
import com.taobao.api.FileItem;
import com.taobao.api.internal.util.WebUtils;
import org.jeecg.common.constant.ProcessConstant;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.Msg;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.Text;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.taobao.api.ApiException;

import java.io.IOException;
/**
 * 
* @ClassName: DingMessageUtils 
* @Description: 钉钉工作通知消息接口	
* @author heckerstone heckerstone@163.com
* @date Aug 17, 2020 4:14:23 PM 
*
 */
public class DingMessageUtils {
	
	/**
	 * 
	* @Title: sendDingMessage 
	* @Description: 发送钉钉工作通知到领导（总经理、副总经理） 
	* @author heckerstone heckerstone@163.com
	* @date Aug 17, 2020 4:20:18 PM 
	* @param @param userids 接收人ids，如：1,2,3
	* @param @param content 发送内容
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	public static boolean sendDingMessage(String userids,String content) {
		boolean flag=false;
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_MESSAGE);
			OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
			req.setAgentId(Long.parseLong(ProcessConstant.AGENTID));
			req.setUseridList(userids);
			Msg obj1 = new Msg();
			obj1.setMsgtype("text");
			Text obj2 = new Text();
			obj2.setContent(content);
			obj1.setText(obj2);
			req.setMsg(obj1);
			OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, DingTalkUtils.get_token());
			System.out.println(rsp.getBody());
			flag=true;
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return flag;
	}


	/**
	 * 上传媒体文件
	 * @param filePath
	 * @return
	 */
	public static String sendDdUpFile(String filePath) {
		boolean flag=false;
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_UPFILEMT);
			OapiMediaUploadRequest req = new OapiMediaUploadRequest();
			req.setType("file");
			// 要上传的媒体文件
			FileItem item = new FileItem(filePath);
			req.setMedia(item);
			OapiMediaUploadResponse rsp = client.execute(req, DingTalkUtils.get_token());
			if(rsp.getErrcode().equals(0L)){
				System.out.println(rsp);
				return rsp.getMediaId();
			}else{
				System.out.println("上传文件失败：错误code："+rsp.getErrcode());
			}

		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
/**
	 * 钉盘管理--单步文件上传
	 * @param filePath
	 * @return
	 */
	public static String sendDdUpFilew(String filePath) {
		boolean flag=false;
		try {
			OapiFileUploadSingleRequest request = new OapiFileUploadSingleRequest();
			FileItem item= new FileItem(filePath);
			request.setFileSize(item.getFileLength());
			request.setAgentId(ProcessConstant.AGENTID);
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/single?"+ WebUtils.buildQuery(request.getTextParams(),"utf-8"));
			// 必须重新new一个请求
			request = new OapiFileUploadSingleRequest();
			request.setFile(item);
			OapiFileUploadSingleResponse rsp = client.execute(request, DingTalkUtils.get_token());
			System.out.println(rsp.getBody());
			if(rsp.getErrcode().equals(0L)){
				System.out.println(rsp);
				return rsp.getMediaId();
			}else{
				System.out.println("上传文件失败：错误code："+rsp.getErrcode());
			}

		} catch (ApiException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 发送文件给指定用户
	 * @param userId 指定用户id
	 * @param type 文件类型
	 * @param media 钉盘管理文件上传返回的唯一表示
	 * @return
	 */
	public static boolean sendDingMessageByType(String userId,String type,String media) {
		boolean flag=false;
		try {
			//TODO   发送钉盘文件给指定用户
			OapiCspaceAddToSingleChatRequest req = new OapiCspaceAddToSingleChatRequest();
			req.setAgentId(ProcessConstant.AGENTID);
			req.setUserid(userId);
			req.setMediaId(media);
			req.setFileName(type);
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_CSPACE+"?" + WebUtils.buildQuery(req.getTextParams(), "utf-8"));
			OapiCspaceAddToSingleChatResponse rsp = client.execute(req, DingTalkUtils.get_token());
			if (!rsp.getErrcode().equals(0L)) {
				System.out.println("钉钉操作失败！code：" + rsp.getErrcode() + "msg:" + rsp.getErrmsg());
			}
			System.out.println(rsp.getBody());
			flag=true;
		} catch (ApiException | IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

}
